<?php
class special_categoryModel extends baseModel{
    protected $table = 'special_category';

    //专题分类列表
    public function special_list($id=0) {
        $data=$this->select('','','sequence desc');
        $cat = new Category(array('cid', 'pid', 'name', 'cname'));
        return $cat->getTree($data, $id);
    }

    //字段保存格式化
    public function common_data_save($data){
        $cid=intval($data['cid']);
        $data['urlname']=$this->get_urlname($data['name'],$data['urlname'],$cid);
        $data['seo_content']=html_in($data['seo_content']);
        $data['sequence']=intval($data['sequence']);
        return $data;
    }

    //专题分类信息
    public function info($cid) {
        return $this->find('cid='.$cid);
    }
    //添加专题分类
    public function add($data)
    {
        //格式化部分字段
        $data=$this->common_data_save($data);
        $cid=$this->insert($data);
        api('editor','relation',array('table'=>'special','file_id'=>$data['file_id'],'id'=>$cid));
        return $cid;
    }
    //编辑专题分类
    public function edit($data)
    {
        //格式化部分字段
        $data=$this->common_data_save($data);
        $condition['cid']=intval($data['cid']);
        api('editor','relation',array('table'=>'special','file_id'=>$data['file_id'],'id'=>$condition['cid']));
        return $this->update($condition,$data); 
    }
    //删除专题分类
    public function del($cid)
    {
        //删除关联
        api('editor','del_file',array('table'=>'special','id'=>$cid));
        model('special')->class_del($cid);
        return $this->delete('cid='.intval($cid)); 
    }

    //获取专题分类拼音
    public function get_urlname($name='', $urlname = null, $cid = null)
    {
        if(empty($name)){
            return false;
            exit;
        }
        if (empty($urlname))
        {
            $pinyin = new Pinyin();
            $name = preg_replace('/\s+/', '-', $name);
            $pattern = '/[^\x{4e00}-\x{9fa5}\d\w\-]+/u';
            $name = preg_replace($pattern, '', $name);
            $urlname = substr($pinyin->output($name, true),0,30);
            if(substr($urlname,0,1)=='-'){
                $urlname=substr($urlname,1);
            }
            if(substr($urlname,-1)=='-'){
                $urlname=substr($urlname,0,-1);
            }
        }

        $where='';
        if (!empty($cid))
        {
            $where = 'AND cid<>' . $cid;
        }
        
        $info = $this->count("urlname='".$urlname."'" .$where); 

        if (empty($info))
        {
            return $urlname;
        }
        else
        {
            return $urlname.substr(cp_uniqid(),8);
        }
    }

    //专题导航
    public function nav($cid)
    {
        $data = $this->select('','cid,pid,name,urlname');
        $cat = new Category(array(
            'cid',
            'pid',
            'name',
            'urlname',
            'cname'));
        if(empty($data)){
             return;
        }
        $i=0;
        foreach ($data as $key => $value) {
            $i++;
            $list[$key]=$value;
            $list[$key]['curl']=model('special_label')->get_curl($value);
            $list[$key]['i']=$i;
            
        }
        return $cat->getPath($list, $cid);
    }

    //专题栏目树
    public function getcat($cid)
    {
        $id = $cid;
        $data = $this->select('','cid,pid,name');
        $cat = new Category(array(
            'cid',
            'pid',
            'name',
            'cname')); //初始化无限分类

        $cat_for = $cat->getTree($data, $id); //获取分类数据树结构
        if(empty($cat_for)){
            return $id;
        }
        foreach ($cat_for as $v) {
            $cat_id .= $v['cid'] . ",";
        }

        if (!empty($cat_id)) {
            return $cat_id . $id;
        } else {
            return $id;
        }
    }

	
}